package visualization;

import java.awt.Color;
import java.awt.Paint;

import model.OligoSystemGeneral;
import optimizers.ganeat.Connection;
import optimizers.ganeat.Individual;
import optimizers.ganeat.Node;

import org.apache.commons.collections15.Transformer;

public class MyEdgeTransformer<String, Paint> implements Transformer<String, Paint> {

	double[][] TimeSerie;
	Individual indiv;
	OligoSystemGeneral oligo;
	
	public MyEdgeTransformer(double[][] TimeSerie, Individual indiv, OligoSystemGeneral oligo) {
		this.TimeSerie = TimeSerie;
		this.indiv = indiv;
		this.oligo = oligo;
	}
	
	@Override
	public Paint transform(String i){
	String From = (String) "";
	String To = (String) "";
	Connection con = null;
	double FInh = 0;
	boolean Cut = false;
	for(Connection conn : indiv.connectionGenes){
	if( i.equals(conn.from.name + conn.to.name) ){
		From = (String) conn.from.name;
		To = (String) conn.to.name;
		con = conn;
		if(indiv.getNode("I" + From + To) != null){
			Node Inh = indiv.getNode("I" + From + To);
			FInh = TimeSerie[oligo.nameToInt.get(Inh.name)][oligo.t] * Inh.parameter;
		}
	}
	}
	
	float[] colorVect = {0,0,0};
	double F = TimeSerie[oligo.nameToInt.get(From)][oligo.t] * con.parameter;
	F = F - FInh > 0 ? F - FInh : 0;
	colorVect[0] = (float)( Math.pow(F*10, 0.05) / Math.pow(oligo.Fmax*10, 0.05) ) * 255;
	colorVect[2] = 255 - (float)( Math.pow(F*10, 0.05) / Math.pow(oligo.Fmax*10, 0.05) ) * 255;
		
	if(colorVect[0] > 255)
	colorVect[0] = 255;
	if(colorVect[2] < 0)
	colorVect[2] = 0;
	if(colorVect[0] == 0)
	Cut = true;
	//System.err.println("RGB pour " + i + " :    " + (int)colorVect[0] + "," + (int)colorVect[1] + "," + (int)colorVect[2] + ",   t = " + t);
	colorVect = Color.RGBtoHSB((int)colorVect[0], (int)colorVect[1], (int)colorVect[2], colorVect);
	Paint color = (Paint) Color.getHSBColor(colorVect[0],colorVect[1],colorVect[2]);
	if(Cut)
	color = (Paint) Color.GRAY;
	return color;
	}

}
